<?php


namespace App\Http\Controllers\admin\service;


use App\Http\Controllers\admin\model\AdminModel;
use App\Http\Controllers\admin\model\VipModel;
use App\Http\Controllers\utils\LogUtil;
use App\Http\Controllers\utils\ToolUtil;

class VipService
{
    /**
     * 增加vip信息
     * @param $param
     * @return false|string
     */
    public static function addVip($param){
        $param['remark'] = '增加vip信息';
        $data = [
            'name'=>$param['name'],
            'vip_size'=>$param['vipSize'],
            'vip_num'=>$param['vipNum'],
            'upgrade'=>$param['upgrade'],
            'create_time'=>time()
        ];
        $int = VipModel::add($data);
        if($int){
            $param['details'] = ToolUtil::handleSuccessOrError(['name'=>$param['name']]);
            LogUtil::addAdminLog($param);
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 获取vip信息
     * @param $param
     * @return false|string
     */
    public static function queryVip($param){
        $admin = AdminModel::queryFirst(['id'=>$param['userId']],['group_id']);
        if($admin['group_id']!=1){
            return ToolUtil::returnJson(400,'没有数据');
        }
        switch ($param['type']){
            case 1:
                $data = self::handleQueryVip($param);
                break;
            case 2:
                $param['field'] = 'name';
                $data = self::handleQueryVip($param);
                break;
            default:
                $data['data'] = null;
        }
        if(count($data['data'])){
            return ToolUtil::returnJson(200,'成功',$data['data'],$data['count']);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 处理获取vip信息
     * @param $param
     * @return mixed
     */
    public static function handleQueryVip($param){
        $data['count'] = VipModel::queryCount([],$param);
        $page = ToolUtil::calculationPage($param,$data['count']);
        $data['data'] = VipModel::queryAll([],$param,$page);
        return $data;
    }

    /**
     * 编号获取vip信息
     * @param $param
     * @return false|string
     */
    public static function queryIdVip($param){
        $field = ['id','name','vip_size','vip_num','upgrade'];
        $data = VipModel::queryFirst(['id'=>$param['id']],$field);
        if($data){
            return ToolUtil::returnJson(200,'成功',$data);
        }
        return ToolUtil::returnJson(400,'没有数据');
    }

    /**
     * 编辑vip信息
     * @param $param
     * @return false|string
     */
    public static function updateVip($param){
        $param['remark'] = '编辑vip信息';
        $data = [
            'name'=>$param['name'],
            'vip_size'=>$param['vipSize'],
            'vip_num'=>$param['vipNum'],
            'upgrade'=>$param['upgrade'],
            'update_time'=>time()
        ];
        $int = VipModel::edit(['id'=>$param['id']],$data);
        if($int){
            $param['details'] = ToolUtil::handleSuccessOrError(['name'=>$param['name']]);
            LogUtil::addAdminLog($param);
            return ToolUtil::returnJson(200,'成功');
        }
        return ToolUtil::returnJson(300,'失败');
    }

    /**
     * 删除vip信息
     * @param $param
     * @return false|string
     */
    public static function deleteVip($param){
        $param['remark'] = '删除vip信息';
        $admin = AdminModel::queryFirst(['vip_id'=>$param['id']],['id']);
        if($admin){
            return ToolUtil::returnJson(301,'有用户正使用该信息,清除后再进行操作');
        }
        $vip = VipModel::queryFirst(['id'=>$param['id']],['name']);
        if($vip){
            $int = VipModel::delete(['id'=>$param['id']]);
            if($int){
                return ToolUtil::returnJson(200,'成功');
            }
            return ToolUtil::returnJson(300,'失败');
        }
        return ToolUtil::returnJson(302,'没有该信息');
    }
}